package com.example.paging

import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.observe
import androidx.paging.ExperimentalPagingApi
import androidx.paging.LoadState
import androidx.paging.PagingData
import androidx.recyclerview.widget.LinearLayoutManager
import com.colaman.statuslayout.StatusConfig
import com.colaman.statuslayout.StatusLayout
import com.example.paging.entity.HomeVO
import com.example.paging.ui.*
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch

class MainActivity : AppCompatActivity() {

    private val viewModel by lazy {
        ViewModelProvider(this).get(HomeVoViewModel::class.java)
    }

//    private val adapter by lazy {
//        HomePageAdapter(this)
//    }

    private val adapter by lazy {
        ArticleAdapter()
    }

    @ExperimentalPagingApi
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        initStatus()

        // 给adapter添加 加载更多功能
        recyclerview.adapter = adapter.withLoadStateFooter(LoadMoreAdapter { adapter.retry() })
        recyclerview.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
        adapter.addDataRefreshListener {
            refreshLayout.isRefreshing = false
        }
        adapter.addLoadStateListener { loadState ->
            // 这里的逻辑可以自由发挥
            when (loadState.refresh) {
                is LoadState.Error -> statusLayout.switchLayout(StatusLayout.STATUS_ERROR)
                is LoadState.Loading -> {
                    statusLayout.showDefaultContent()
                    refreshLayout.isRefreshing = true
                }
                is LoadState.NotLoading -> {
                    statusLayout.showDefaultContent()
                    refreshLayout.isRefreshing = false
                }
            }
        }

        refreshLayout.setOnRefreshListener {
            // 刷新数据
            adapter.refresh()
        }


        lifecycleScope.launch {
            viewModel.pager.collect {
//                adapter.submitData(it.map { ItemArticleView(it) as PagingItemView<Any> })
                adapter.submitData(it)
            }
        }

    }


    /**
     * 初始化 StatusLayout
     */
    private fun initStatus() {
        statusLayout.add(StatusConfig(StatusLayout.STATUS_LOADING, R.layout.layout_loading))
        statusLayout.add(
            StatusConfig(
                StatusLayout.STATUS_ERROR,
                R.layout.layout_error,
                clickRes = R.id.btn_retry
            )
        )
        statusLayout.setLayoutClickListener(object : StatusLayout.OnLayoutClickListener {
            override fun OnLayoutClick(view: View, status: String?) {
                if (status === StatusLayout.STATUS_ERROR) {
                    adapter.retry()
                }
            }
        })
    }


}



